%%% calculates non-monotonicity point for each pair of binary lotteries
clc
clear
cd ''

global a1 b1 p_a1 a2 b2 p_a2

CARA=0;
ep=0;

run indifference_threshold_calculation
theta_xy=omega_th_final1';
lottery_n=size(theta_xy,2);

% lottery one is the safer one
a1=lotteries(:,1);
b1=lotteries(:,2);
p_a1=lotteries(:,3);
% lottery 2 is the riskier one
a2=lotteries(:,4);
b2=lotteries(:,5);
p_a2=lotteries(:,6);

column=1
for k=0.01:.01:2
    %%% With risk_av=-2; row 1--> th=-2; row 101 --> th=-1 row 201-->th=0; row
    %%% 301-->th=1 etc.
    risk_av=-2;
    sig_th=k;


    for j=1:701

        for l=1:lottery_n
            if risk_av~=1
                %%% utility of option a in the less risky lottery 1 (referred to  as X in the
                %%% paper)
                U_1_a=(a1(l).^(1-risk_av))./(1-risk_av);
                %%% utility of option b in the less risky lottery 1 (referred to  as Y in the
                %%% paper)
                U_1_b=(b1(l).^(1-risk_av))./(1-risk_av);
            else
                U_1_a=log(a1(l));
                %%% utility of option a in the less risky lottery 1 (referred to  as X in the
                %%% paper)
                U_1_b=log(b1(l));
                %%% utility of option b in the less risky lottery 1 (referred to  as X in the
                %%% paper)
            end

            %%% Expected Utility of lottery 1
            U_1_E=p_a1(l).*U_1_a+(1-p_a1(l)).*U_1_b;

            if risk_av~=1
                %%% utility of option a in the riskier lottery 2 (referred to as Y in the
                %%% paper)
                U_2_a=(a2(l).^(1-risk_av))./(1-risk_av);
                %%% utility of option b in the riskier lottery 2 (referred to as Y in the
                %%% paper)
                U_2_b=(b2(l).^(1-risk_av))./(1-risk_av);
            else
                %%% utility of option a in the riskier lottery 2 (referred to as Y in the
                %%% paper)
                U_2_a=log(a2(l));
                %%% utility of option b in the riskier lottery 2 (referred to as Y in the
                %%% paper)
                U_2_b=log(b2(l));
            end

            %%% Expected Utility of lottery 2
            U_2_E=p_a2(l).*U_2_a+(1-p_a2(l)).*U_2_b;

            %%% Gives utility difference at a given level of risk aversion
            u_diff(l)=U_2_E-U_1_E;
        end
        %%%%%%%%%%%%%%%%%%%% End of EU calculation  %%%%%%%%%%%%%%%%%%%%%%%%%%

        %%% assumes normally distributed additive shock on utility difference
        %%% N~(0,sig_th^2), where sig_th=k


        for l=1:lottery_n
            temp(1,l)=u_diff(:,l)./sig_th;
        end
        %%% Probability of choosing the risky option given an additive
        %%% random utility shock (aRUM)
        p_risky=normcdf(temp)';

        result_table(:,j)=p_risky;
        risk_av=-2+j/100  
    end


    %%% Calculate reversal points for each Q, at which the predicted
    %%% probability of choosing the riskier option stops falling and starts
    %%% rising.   
    for m=1:size(result_table,1)
        revert_point(m,column)=NaN;            
        for i=2:size(result_table,2)
            if result_table(m,i)<=result_table(m,i-1)
                %%% Transofrms column numbers to risk aversion
                revert_point(m,column)=(i-201)/100;
            end
        end

    end
    column=column+1;

end

nm_table=min(revert_point');
% set indifference thresholds to infinity for dominated options
nm_table([10 20 30])=Inf;

%%% Replicates Table 1 of the online Appendix, in conjunction with risk
%%% aversion estimates at the 50th, 75th, and 90th percentiles.
comp=[theta_xy' nm_table'];










